import type { SearchFormSchema } from '@/components/searchform';
import { Selector } from 'gantd';
import request from '@/utils/request';
import { getRecipientListWithName } from './util';
import { uniqueArr } from '@/pages/xbommanage/SpecialOrder/page/XbomObjectCreate/util';

export const tableSchema: any = [
  {
    fieldName: 'ebomRoomCode',
    title: 'EBOM房间',
    editConfig: {
      component: Selector,
      editable: true,
      signable: true,
      rules: {
        required: true,
        whitespace: true,
        message: 'EBOM房间必选',
      },
      props: {
        // 表格刷新再state更新后加载
        query: async function () {
          const ls = await request.post('/mstdata/productNode/findProduct', {
            data: {
              filterInfo: {},
              pageInfo: { beginIndex: 0, pageSize: 1500 },
            },
          });
          const finallLs = ls.map((item) => {
            return {
              ...item,
              showName: `${item.nodeCode}（${item.nodeName}）`,
            };
          });
          return uniqueArr(finallLs || [], 'nodeCode');
        },
        labelProp: 'showName', // 下拉时候的展示
        valueProp: 'nodeCode', // 表格单元格的实际值
        useStorage: true,
        useCache: true,
      },
      editable: true,
    },
  },
  {
    fieldName: 'plantName',
    title: '工厂名称',
    editConfig: {
      component: Selector,
      rules: {
        required: true,
        whitespace: true,
        message: '工厂名称必选',
      },
      props: (params) => {
        return {
          query: async () => {
            if (params?.ebomRoomCode) {
              // 获取工厂列表
              const res = await request('/bommgmt/custReceivedVehicle/findCustByCriteria', {
                method: 'POST',
                data: {
                  filterInfo: {
                    bomType: 'MBOM',
                    ebomRoomCode: params?.ebomRoomCode,
                  },
                  pageInfo: {
                    pageSize: 1500,
                    beginIndex: 0,
                  },
                },
              });
              return uniqueArr(res || [], 'plantId');
            }
          },
          labelProp: 'name',
          valueProp: 'name',
          useStorage: false,
          useCache: false,
          allowClear: true,
        };
      },
      editable: true,
    },
  },
  {
    fieldName: 'bomRoom',
    title: 'MBOM房间',
    width: 180,
    editConfig: {
      type: 'string',
      editable: false,
    },
  },
  {
    fieldName: 'mbomEngineerList',
    title: '默认MBOM工程师',
    width: 180,
    valueFormatter: (params: any) => {
      return getRecipientListWithName(params?.value);
    },
  },
  {
    fieldName: 'interfacePerson',
    title: '默认接口人',
    flex: 1, // 顶满整行
    valueFormatter: (params: any) => {
      return getRecipientListWithName(params?.value);
    },
  },
];

export const searchSchema: SearchFormSchema = {};
